package com.cloudptt.wp.ui.call;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

import com.cloudptt.api.product.NetworkListener;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.adapter.ICallInfoDesc;
import com.cloudptt.api.product.func.sys.SysStateInfo;
import com.cloudptt.api.product.func.sys.listener.SysListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.mcptt.db.model.AddrInfo;

public class FloorDlg extends Activity implements ICallInfoDesc, NetworkListener, SysListener {
    private static final String TAG = "FloorDlg";
    public static final String KEY = "addrStr";
//    private Context context = null;
    private TextView floorTv =null;
    private TextView secTv =null;
//    public static boolean isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_floor);

        TextView addrTv = (TextView) findViewById(R.id.addrTv);
        String dscp = getIntent().getStringExtra(KEY);
        addrTv.setText(dscp);
        floorTv = (TextView) findViewById(R.id.floorTv);
        secTv = (TextView) findViewById(R.id.secTv);
//        endBtn = (Button)findViewById(R.id.endBtn);
//        SysApplication app = ((SysApplication) context.getApplicationContext());
//        Product product = app.getProduct();
//        endBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // TODO Auto-generated method stub
//                if(product != null)
//                {
//                    product.setCurGroupInfo(null);
//                }
//                finish();
//            }
//        });
        this.setFinishOnTouchOutside(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        isShow = true;
        SysApplication app = ((SysApplication) this.getApplicationContext());
        Product product = app.getProduct();
        if(product != null)
        {
            product.setNetworkListener(this);
            product.setSysListener(this);
            product.getGCallEventAdapter().setCallInfoDesc(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        isShow = false;
//        Log.d(TAG, "onStop isShow = "+ isShow);
        SysApplication app = ((SysApplication) this.getApplicationContext());
        Product product = app.getProduct();
        if(product != null)
        {
            AddrInfo oldDefaultGroupInfo = product.getOldDefaultGroupInfo();
            Log.d(TAG, "onStop oldDefaultGroupInfo = " + oldDefaultGroupInfo);
            if(oldDefaultGroupInfo != null) {
                product.setDefaultGroupInfo(oldDefaultGroupInfo);
            }
            product.setCurGroupInfo(null);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown keyCode "+ keyCode);

        SysApplication app = ((SysApplication) this.getApplicationContext());
        Product product = app.getProduct();
        if (keyCode == KeyEvent.KEYCODE_SOFT_LEFT
                || keyCode == 66
                || keyCode == 1
                ||keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(product != null)
            {
                AddrInfo oldDefaultGroupInfo = product.getOldDefaultGroupInfo();
                Log.d(TAG, "onStop oldDefaultGroupInfo = " + oldDefaultGroupInfo);
                if(oldDefaultGroupInfo != null) {
                    product.setDefaultGroupInfo(oldDefaultGroupInfo);
                }
                product.setCurGroupInfo(null);
            }
//            isShow = false;
            finish();
        }
        return true;
    }

    @Override
    public boolean isLogined() {
        return false;
    }

    @Override
    public void networkChange(boolean isConnect) {

    }

    @Override
    public void updateCallUser(String callKey, boolean isShow, String userDesc) {
        Log.d(TAG, "updateCallUser callKey = " + callKey + " isShow = " + isShow + " userDesc = " + userDesc);
    }

    @Override
    public void updateCallInfoDesc(String callKey, String gNum, String callInfoDesc) {
        Log.d(TAG, "updateCallUser callKey = " + callKey + " gNum = " + gNum + " callInfoDesc = " + callInfoDesc);
        floorTv.setText(callInfoDesc);
    }

    @Override
    public void updateSecDesc(String callKey, String secStr) {
        Log.d(TAG, "updateCallUser callKey = " + callKey + " secStr = " + secStr);
        secTv.setText(secStr);
    }

    @Override
    public void onSysState(SysStateInfo stateInfo) {
        Log.d(TAG, "onSysState stateInfo = " + stateInfo);
        int state = stateInfo.getState();
//		int cause = stateInfo.getCause();
        if(SysStateInfo.SysState_SipAuthSucState != state)
        {
            finish();
        }
    }

    @Override
    public void onSipAuthResult(int code) {

    }

    @Override
    public void onForceDetach() {

    }

    @Override
    public void onKeepAlive(boolean isconnected) {

    }

    @Override
    public void onSvrRestart() {

    }
}
